package com.lm.activiti.boot.datatype;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.*;
import com.lm.activiti.boot.dto.Animal;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;

import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author lm_jcf
 * @date 2018/9/19 17:59
 */
public class MultimapsTest {

    public static void main(String[] args) {
        Multimap<String, String> multimaps = LinkedHashMultimap.create();

        String key = "key";
        multimaps.put(key, "第一名");
        multimaps.put(key, "第二名");
        multimaps.put(key, "第四名");
        multimaps.put(key, "第五名");
        multimaps.put("离人", "林志炫");

        /*System.out.println(multimaps.containsValue("第二名"));

        System.out.println(multimaps.size());

        Collection<String> strings = multimaps.get(key);
        for (String string : strings) {
//            System.out.println(string);
        }

        for (String s : multimaps.keySet()) {
            Collection<String> strings1 = multimaps.get(s);
            Iterator<String> iterator = strings1.iterator();
            while (iterator.hasNext()) {
                String next = iterator.next();
                System.out.println(next);
            }
        }*/

        List<String> strings1 = Lists.newArrayList(multimaps.get("key"));
        System.out.println(strings1);

        System.out.println(JSON.toJSONString(multimaps.get("dd")));
    }

    @Test
    public void mu(){

        ArrayList<Animal> objects = Lists.newArrayList();

        objects.add(new Animal("猫1", 10));
        objects.add(new Animal("猫1", 10));
        objects.add(new Animal("猫1", 10));
        objects.add(new Animal("猫2", 10));

        ImmutableListMultimap<String, Animal> index = Multimaps.index(objects, Animal::getName);

        System.out.println(index.keySet());
    }

    @Test
    public void multiMap(){

        Multimap<String, Animal> multiMaps = HashMultimap.create();
        System.out.println(JSON.toJSONString(multiMaps));

        Animal cat = new Animal("猫1", 10);
        multiMaps.put("猫", new Animal("猫1", 10));
        multiMaps.put("猫", new Animal("猫2", 11));
        multiMaps.put("猫", new Animal("猫2", 11));

        multiMaps.put("猫", cat);
        multiMaps.put("猫", cat);

        System.out.println(multiMaps.values());

        List<Animal> list = Lists.newArrayList();
        list.addAll(multiMaps.get("ceshi"));
        System.out.println(list);


        Collection<Animal> values = multiMaps.values();

        ImmutableListMultimap<String, Animal> index = Multimaps.index(values, Animal::getName);

        for (String key : index.keySet()) {
            ImmutableList<Animal> animals = index.get(key);

            System.out.println(JSON.toJSONString(animals));

        }

        System.out.println(index.keySet());

        Map<Integer, Integer> collect = list.stream().collect(Collectors.toMap(Animal::getAnInt, Animal::getAnInt));

    }

    @Test
    public void coverToMap() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {

        Animal cat = new Animal();

        Map<String, String> describe = BeanUtils.describe(cat);

        System.out.println(JSON.toJSONString(describe));
    }


}
